//function removeChildren(pnode){    
//	var childs=pnode.childNodes;    
//	for(var i=childs.length-1;i>=0;i--){    
//		pnode.removeChild(childs.item(i));    
//	}    
//}   ｓｓ

//搜索显示切换
$(function(){
	function getLocalTime(nS) {  
	 	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');  
	}

	function formatSeconds(value) {
				var theTime = parseInt(value); // 秒
				var theTime1 = 0; // 分
				var theTime2 = 0; // 小时
				if (theTime > 60) {
					theTime1 = parseInt(theTime / 60);
					theTime = parseInt(theTime % 60);
					if (theTime1 > 60) {
						theTime2 = parseInt(theTime1 / 60);
						theTime1 = parseInt(theTime1 % 60);
					}
				}
				var result = "" + parseInt(theTime) + "秒";
				if (theTime1 > 0) {
					result = "" + parseInt(theTime1) + "分" + result;
				}
				if (theTime2 > 0) {
					result = "" + parseInt(theTime2) + "小时" + result;
				}
			return result;
			}


	var removeChildNodes = function($, own) {
			if ($ == null) {
				return;
			}
			while ($.hasChildNodes()) {
				$.removeChild($.firstChild);
			}
			if (own) {
				$.parentNode.removeChild($);
			}
	};
	var $url='http://192.168.0.12:8080/elink_video';	
	$(function(e) {
	   var $area = $('.search-area'),
	        $showhide = $('.showhide-search'),
	        $input = $('.search-input');      
	    $('.course-nav-hd').on('click', '.showhide-search', function (event){
	        var isShow = $(this).attr('data-show');
	        if( isShow == 'no'){
	            show()
	            $input.focus();
	            $input.keyup(function(){
	            	$(".search-result").css("display","block");
	            });
	            $(".search-input").css("border","1px solid #bcbcbc");
	        }else{
	        	if($input.val()==""){
	        		hide()
	        	}
	        }                    
	        return false;
	    })        
	    $('.course-nav-hd').on('click', '.search-area', function (event){
	        return false;
	    })         
	    $(document).on('click', function (){
	        if( $input.val() != ''){
	            hide()
	        }else{
	        	hide()
	        }
	    })       
	    var show = function (){
	        $showhide.attr('data-show', 'yes')
	        $area.show(1, function (){ $area.removeClass('min') })
	    }     
	    var hide = function (){
	        $showhide.attr('data-show', 'no')
	        $area.fadeOut('slow', function (){
	            $area.addClass('min')
	        })
	    }
		})
	
	//点击显示当前状态
	$(".course-nav-item").click(function(){
		$(this).addClass("on").siblings().removeClass("on");	
	})
	
	$(".newest").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
	})
	
	$(".hide-text").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
	})
	
	$(".page a").click(function(){
		$(this).addClass("active").siblings().removeClass("active");
	})
	
	//課程方向
	$.ajax({ 
        type:"GET",
        dataType:"json",
        url:$url+'/api/getMainType/'+0,
        //contentType: 'application/json;charset=UTF-8',
        beforeSend: function () {
							
		},
        success: function(result) {
      		  	var items = result.data.list;
      		  	$.each(items, function(i,commodity){
      		  		$('<li/>',{
      		  			html:'<a href="javascript:;">'+commodity.mainTypeName+'</a>'
      		  		}).appendTo('.'+'ul').addClass("course-nav-item").on('click',function(){
      		  			$(this).attr("id",commodity.mainTypeId);
      		  			$(this).addClass("on").siblings().removeClass("on"); 
      		  			$(this).siblings().removeAttr("id");    		  			
							removeChildNodes(document.getElementById("bd"));
      		  			$.ajax({
					        type:"GET",
					        dataType:"json",
					       	url:$url+'/api/getChildType/'+$(this).attr("id"),
					        success: function(result) {
					      		  	var items = result.data.list;
					      		  	$.each(items, function(i,commodity){
					      		  		$('<li/>',{				      		  			
					      		  			html:'<a href="javascript:;">'+commodity.childTypeName+'</a>'
					      		  		}).appendTo('.'+'ul-course').addClass("course-nav-item").on('click',function(){
					      		  			$(this).attr("id",commodity.childTypeid);
					      		  			$(this).addClass("on").siblings().removeClass("on");
					      		  			$(this).siblings().removeAttr("id");
					      		  			removeChildNodes(document.getElementById("ul-course4"));
					      		  			$.ajax({
										        type:"GET",
										        dataType:"json",
										       	url:$url+'/api/getVideoList/1/1',
										        success: function(result) {
										      		  	var items = result.data.list;
										      		  	$.each(items, function(i,commodity){
										      		  		$('<li/>',{	      		  			
										      		  			html:
										      		  			'<a href="javascript:;">'+
										      		  			'<div class="figure">'+
										      		  			'<img src="http://192.168.0.12:8080/elink_video/resources'+commodity.snapshot_url+'"/>'+
										      		  			'</div>'+
										      		  			'<h5 class="title">'+commodity.video_source_name+
										      		  			'<span class="fr">'+formatSeconds(commodity.videoLength)+
										      		  			'</span>'+
										      		  			'</h5>'+
										      		  			'<p class="desc">'+commodity.describe+
										      		  			'</p>'+
										      		  			'<div class="author-warp">'+
										      		  			'<span class="author fl">'+commodity.videoSourceId+
										      		  			'</span>'+
										      		  			'<span class="time">'+getLocalTime(commodity.create_time)+
										      		  			'</span>'+
										      		  			'<span class="num fr">播放量：'+
										      		  			'<em>'+commodity.play_times+
										      		  			'</em>'+
										      		  			'</span>'+
										      		  			'</div>'+
										      		  			'</a>'
										      		  		}).appendTo('.'+'ul-course4').addClass("course-nav-item").on('click',function(){
										      		  			$(this).attr("id",commodity.childTypeid);
										      		  			$(this).siblings().removeAttr("id");
										      		  		});
										      		  	});
										      	},
										        error:function(jqXHR){     
										           alert("发生错误：" + jqXHR.status)  
										        }  
											});
					      		  		});					      		  		
					      		  	});
					      	},
					        error:function(jqXHR){     
					           alert("发生错误：" + jqXHR.status)  
					        }  
						});
      		  		});
      		  	});
      	},
        error:function(jqXHR){     
           alert("发生错误：" + jqXHR.status)  
        }  
	});
	
	$.ajax({
        type:"GET",
        dataType:"json",
       	url:$url+'/api/getChildType/1',
        success: function(result) {
      		  	var items = result.data.list;
      		  	$.each(items, function(i,commodity){
      		  		$('<li/>',{				      		  			
      		  			html:'<a href="javascript:;">'+commodity.childTypeName+'</a>'
      		  		}).appendTo('.'+'ul-course').addClass("course-nav-item").on('click',function(){
      		  			$(this).attr("id",commodity.childTypeId);
      		  			//alert(commodity.childTypeId);
      		  			
      		  			$(this).addClass("on").siblings().removeClass("on");
      		  			$(this).siblings().removeAttr("id");
      		  			removeChildNodes(document.getElementById("ul-course4"));
      		  			$.ajax({
					        type:"GET",
					        dataType:"json",
					       	url:$url+'/api/getVideoList/'+$(this).attr("id")+'/'+1,
					        success: function(result) {
					      		  	var items = result.data.list;
					      		  	$.each(items, function(i,commodity){
					      		  		$('<li/>',{	      		  			
					      		  			html:
					      		  			'<a href="javascript:;">'+
					      		  			'<div class="figure">'+
					      		  			'<img src="http://192.168.0.12:8080/elink_video/resources'+commodity.snapshot_url+'"/>'+
					      		  			'</div>'+
					      		  			'<h5 class="title">'+commodity.video_source_name+
					      		  			'<span class="fr">'+formatSeconds(commodity.videoLength)+
					      		  			'</span>'+
					      		  			'</h5>'+
					      		  			'<p class="desc">'+commodity.describe+
					      		  			'</p>'+
					      		  			'<div class="author-warp">'+
					      		  			'<span class="author fl">'+commodity.videoSourceId+
					      		  			'</span>'+
					      		  			'<span class="time">'+getLocalTime(commodity.create_time)+
					      		  			'</span>'+
					      		  			'<span class="num fr">播放量：'+
					      		  			'<em>'+commodity.play_times+
					      		  			'</em>'+
					      		  			'</span>'+
					      		  			'</div>'+
					      		  			'</a>'
					      		  		}).appendTo('.'+'ul-course4').addClass("course-nav-item").on('click',function(){
					      		  			$(this).attr("id",commodity.childTypeid);
					      		  			$(this).siblings().removeAttr("id");
					      		  		});
					      		  	});
					      	},
					        error:function(jqXHR){     
					           alert("发生错误：" + jqXHR.status)  
					        }  
						});
      		  		});
      		  	});
      	},
        error:function(jqXHR){     
           alert("发生错误：" + jqXHR.status)  
        }  
	});
	
	$.ajax({
        type:"GET",
        dataType:"json",
       	url:$url+'/api/getVideoList/1/1',
        success: function(result) {
      		  	var items = result.data.list;
      		  	$.each(items, function(i,commodity){
      		  		$('<li/>',{	      		  			
      		  			html:
      		  			'<a href="javascript:;">'+
      		  			'<div class="figure">'+
      		  			'<img src="http://192.168.0.12:8080/elink_video/resources'+commodity.snapshot_url+'"/>'+
      		  			'</div>'+
      		  			'<h5 class="title">'+commodity.video_source_name+
      		  			'<span class="fr">'+formatSeconds(commodity.videoLength)+
      		  			'</span>'+
      		  			'</h5>'+
      		  			'<p class="desc">'+commodity.describe+
      		  			'</p>'+
      		  			'<div class="author-warp">'+
      		  			'<span class="author fl">'+commodity.videoSourceId+
      		  			'</span>'+
      		  			'<span class="time">'+getLocalTime(commodity.create_time)+
      		  			'</span>'+
      		  			'<span class="num fr">播放量：'+
      		  			'<em>'+commodity.play_times+
      		  			'</em>'+
      		  			'</span>'+
      		  			'</div>'+
      		  			'</a>'
      		  		}).appendTo('.'+'ul-course4').addClass("course-nav-item").on('click',function(){
      		  			$(this).attr("id",commodity.childTypeid);
      		  			$(this).siblings().removeAttr("id");
      		  		});
      		  	});
      	},
        error:function(jqXHR){     
           alert("发生错误：" + jqXHR.status)  
        }  
	});
	
	$(".main-l").on('click',function(){
		$.ajax({
        type:"GET",
        dataType:"json",
       	url:$url+'/api/getAllChildType',
        success: function(result) {
      		  	var items = result.data.list;
      		  	$.each(items, function(i,commodity){
      		  		$('<li/>',{	      		  			
      		  			html:
      		  			'<a href="javascript:;">'+
      		  			'<div class="figure">'+
      		  			'<img src="http://192.168.0.12:8080/elink_video/resources'+commodity.snapshot_url+'"/>'+
      		  			'</div>'+
      		  			'<h5 class="title">'+commodity.video_source_name+
      		  			'<span class="fr">'+formatSeconds(commodity.videoLength)+
      		  			'</span>'+
      		  			'</h5>'+
      		  			'<p class="desc">'+commodity.describe+
      		  			'</p>'+
      		  			'<div class="author-warp">'+
      		  			'<span class="author fl">'+commodity.videoSourceId+
      		  			'</span>'+
      		  			'<span class="time">'+getLocalTime(commodity.create_time)+
      		  			'</span>'+
      		  			'<span class="num fr">播放量：'+
      		  			'<em>'+commodity.play_times+
      		  			'</em>'+
      		  			'</span>'+
      		  			'</div>'+
      		  			'</a>'
      		  		}).appendTo('.'+'ul-course4').addClass("course-nav-item").on('click',function(){
      		  			$(this).attr("id",commodity.childTypeid);
      		  			$(this).siblings().removeAttr("id");
      		  		});
      		  	});
      	},
        error:function(jqXHR){     
           alert("发生错误：" + jqXHR.status)  
        }  
	});
	})
	
	$(".main-f").on('click',function(){
		$(this).css({"color":"#fff","background":"#5ac0eb"});
	})
})
